package com.xd.Controller.Common;
import com.xd.Result.CommonResult;
import com.xd.erroCode.Errcode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import aliOss.Util;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.UUID;


@RequestMapping("/admin/common/")
@Slf4j
@RestController
public class AliOssController {
    @Autowired
    Util aliOssUtil;
    @PostMapping("/upload")
    public CommonResult<String> file(MultipartFile file){
//        得到原始文件名称
        String originalFilename = file.getOriginalFilename();
//        得到文件后缀
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String name = UUID.randomUUID().toString();
//        得到进过uuid转化后的名称
        String objectName=name+suffix;
        try {
            String filPath = aliOssUtil.upload(file.getBytes(), objectName);
            return CommonResult.Success(filPath);
        } catch (IOException e) {
            log.info("异常为"+e);
        }
        return CommonResult.error(Errcode.UPLOAD_FAILED);
    }
}
